package com.woniuxy.d_cache;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author lg
 * @time 2020/07/04 15:29:08
 */
public class MyRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println ("授权");
        Object primaryPrincipal = principalCollection.getPrimaryPrincipal ();

        SimpleAuthorizationInfo sa=new SimpleAuthorizationInfo ();

          if("foo".equals (primaryPrincipal)){
               sa.addStringPermission ("user；save");
               sa.addStringPermission ("user:delete");
          }
        return sa;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        Object principal = authenticationToken.getPrincipal ();
         if(!"foo".equals (principal)){
             return null;

         }

        return new SimpleAuthenticationInfo (principal,"123","myRealm");
    }
}
